package com.mission.schedule.service;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;

import com.mission.schedule.activity.AlarmDialog;
import com.mission.schedule.activity.MainActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.WriteAlarmClock;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.DateTimeHelper;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AlarmService extends Service {

	String alarmType;
	String ringcode;
	String alarmSound;
	int cdId;
	String morningstate;
	String nightstate;
	String alarmclocktime;
	String before;
	String content = "";
	String alltimestate;
	String ringstate;
	String displaytime;
	String postpone;
	String alarmSoundDesc;
	String isalarmtype;
	MediaPlayer mediaPlayer = null;

	SharedPrefUtil sharedPrefUtil = null;

	private Vibrator vibrator;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		if (intent == null) {
			return;
		}
		sharedPrefUtil = new SharedPrefUtil(getApplication(), ShareFile.USERFILE);
		mediaPlayer = new MediaPlayer();
		vibrator = (Vibrator) getApplication().getSystemService(Context.VIBRATOR_SERVICE);
		alarmType = intent.getStringExtra("alarmType");
		isalarmtype = intent.getStringExtra("isalarmtype");
		ringcode = intent.getStringExtra("ringcode");
		alarmSound = intent.getStringExtra("alarmSound");
		alarmSoundDesc = intent.getStringExtra("alarmSoundDesc");
		cdId = intent.getIntExtra("cdId", 0);
		morningstate = intent.getStringExtra("morningstate");
		nightstate = intent.getStringExtra("nightstate");
		alarmclocktime = intent.getStringExtra("alarmclocktime");
		before = intent.getStringExtra("before");
		content = intent.getStringExtra("content");
		alltimestate = intent.getStringExtra("alltimestate");
		ringstate = intent.getStringExtra("ringstate");
		displaytime = intent.getStringExtra("displaytime");
		postpone = intent.getStringExtra("postpone");

		if ("默认".equals(alarmSoundDesc)) {
			alarmSoundDesc = "完成任务";
			ringcode = "g_88";
		}
		if ("".equals(ringcode)) {
			ringcode = "g_88";
		}
		if (cdId < 0) {
			if (cdId == -1) {
				content = "提醒整理您的待办事项";
			} else if (cdId == -10) {
				content = "请打开您的时间表，每五分钟测试一次！";
			} else {
				content = "查看是否还有未完成的事情";
			}
		} else if (cdId > 0 && "0".equals(displaytime)) {
			content = "今天有待办的事情需要完成";
		}

		NotificationManager nm = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
		Intent notificationIntent = new Intent(getApplication(), MainActivity.class);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent contentIntent = PendingIntent.getActivity(getApplication(), 0, notificationIntent, 0);

		if ((cdId >= 0 && "1".equals(displaytime)) || cdId == -10) {
			// 桌面显示提示
			Intent i = new Intent(getApplication(), AlarmDialog.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.putExtra("cdId", String.valueOf(cdId));
			i.putExtra("content", content);
			i.putExtra("alarmclocktime", alarmclocktime);
			getApplication().startActivity(i);
		} else {

			if (alarmSound == null)
				return;

			if ("1".equals(ringstate)) {
				return;
			} else if ("2".equals(ringstate)) {
				ringcode = "g_220";
				alarmSound = "g_220";
			}
			if (cdId >= 0 && "0".equals(displaytime)) {
				alarmType = "1";
				ringcode = "g_220";
				alarmSound = "g_220";
			}

			Calendar cd = Calendar.getInstance();
			cd.setTime(DateUtil.parseDateTime(alarmclocktime));
			cd.add(Calendar.MINUTE, -Integer.parseInt(before));
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			try {
				AssetFileDescriptor fileDescriptor = null;
				if ("3".equals(isalarmtype)) {// >=
					// System.currentTimeMillis()
					if (DateTimeHelper.formatDateTimetoString(cd.getTime(),
							DateTimeHelper.FMT_yyyyMMddHHmm).equals(
							DateTimeHelper.formatDateTimetoString(new Date(),
									DateTimeHelper.FMT_yyyyMMddHHmm))) {
						fileDescriptor = getApplication().getAssets().openFd(ringcode + ".mp3");
					} else {
						fileDescriptor = getApplication().getAssets().openFd(alarmSound + ".mp3");
					}
				} else if ("2".equals(isalarmtype)) {
					if (DateTimeHelper.formatDateTimetoString(cd.getTime(),
							DateTimeHelper.FMT_yyyyMMddHHmm).equals(
							DateTimeHelper.formatDateTimetoString(new Date(),
									DateTimeHelper.FMT_yyyyMMddHHmm))) {
						fileDescriptor = getApplication().getAssets().openFd(ringcode + ".mp3");
					} else {
						fileDescriptor = null;
					}
				} else if ("0".equals(isalarmtype)) {
					fileDescriptor = null;
				} else {
					if (cdId < 0) {
						if (cdId == -1) {
							if ("0".equals(morningstate)) {
								fileDescriptor = getApplication().getAssets().openFd(alarmSound);
							}
						} else if (cdId == -2) {
							if ("0".equals(nightstate)) {
								fileDescriptor = getApplication().getAssets().openFd(alarmSound);
							}
						} else {
							fileDescriptor = getApplication().getAssets().openFd(alarmSound + ".mp3");
						}
					} else {
						fileDescriptor = getApplication().getAssets().openFd(alarmSound + ".mp3");
					}
				}
				if (fileDescriptor == null)
					return;
				mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
						fileDescriptor.getStartOffset(),
						fileDescriptor.getLength());
				// mediaPlayer.prepare();
				// mediaPlayer.start();

				final AudioManager audioManager = (AudioManager) getApplication().getSystemService(Context.AUDIO_SERVICE);
				//if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				mediaPlayer.prepare();
				mediaPlayer.start();
				//}
				try {
					File temp = new File(Environment.getExternalStorageDirectory()
							.getPath() + "/YourAppFolder/");// 自已缓存文件夹
					if (!temp.exists()) {
						temp.mkdir();
					}
					FileWriter fw = new FileWriter(temp.getAbsolutePath()
							+ "/bb.txt", true);
					if (fw != null) {
						fw.flush();
						fw.write(format1.format(new Date()));
						fw.write("  ," + content);
						fw.write("  ," + alarmSound + "\n");
						fw.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (cdId == -10) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtil.parseDateTimeSs(alarmclocktime));
				calendar.add(Calendar.MINUTE, 5);
				App.getDBcApplication().updateClockDate(cdId,
						DateUtil.formatDateTimeSs(calendar.getTime()),
						DateUtil.formatDateTimeSs(calendar.getTime()));
			} else if (cdId == -1 || cdId == -2) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtil.parseDateTimeSs(alarmclocktime));
				calendar.add(Calendar.DATE, 1);
				App.getDBcApplication().updateClockDate(cdId,
						DateUtil.formatDateTimeSs(calendar.getTime()),
						DateUtil.formatDateTimeSs(calendar.getTime()));
			} else if (cdId >= 0 && "1".equals(postpone) && "1".equals(displaytime)) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtil.parseDateTimeSs(alarmclocktime));
				calendar.add(Calendar.DATE, 1);
				App.getDBcApplication().updateClockDate(cdId,
						DateUtil.formatDateTimeSs(calendar.getTime()),
						DateUtil.formatDateTimeSs(calendar.getTime()));
			} else if (cdId >= 0 && "0".equals(displaytime)) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtil.parseDateTimeSs(alarmclocktime));
				calendar.add(Calendar.DATE, 1);
				App.getDBcApplication().updateClockDate(cdId,
						DateUtil.formatDateTimeSs(calendar.getTime()),
						DateUtil.formatDateTimeSs(calendar.getTime()));
			}

			WriteAlarmClock.writeAlarm(getApplicationContext());// 写入闹钟
		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mediaPlayer.stop();

		mediaPlayer.release();

		mediaPlayer = null;
	}
}
